package com.agency8x.web.util;

import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;

/**
 * 控制器辅助类
 * 
 * @author SoChishun
 * @version 1.0 2017-5-17
 */
public class ControllerUtil {
	public static void renderDisptchJumpSuccess(Controller controller) {
		renderDisptchJumpSuccess(controller, "操作成功", null, 1);
	}

	public static void renderDisptchJumpSuccess(Controller controller, String message) {
		renderDisptchJumpSuccess(controller, message, null, 1);		
	}

	public static void renderDisptchJumpSuccess(Controller controller, String message, String url, int seconds) {
		if (null == url || url.isEmpty()) {
			url = "window.parent.location.reload();";
		} else {
			url = "location.href='" + url + "';";
		}
		controller.setAttr("doActionScript", "<script>function doActionScript(){" + url + "}</script>");
		controller.setAttr("message", message);
		controller.setAttr("seconds", seconds);
		controller.setAttr("status", true);
		controller.render("/view/common/dispatch_jump.html");
	}

	public static void renderDisptchJumpError(Controller controller, String message) {
		renderDisptchJumpError(controller, new String[] { message }, null, 3);
	}

	public static void renderDisptchJumpError(Controller controller, String[] messages) {
		renderDisptchJumpError(controller, messages, null, 3);
	}

	public static void renderDisptchJumpError(Controller controller, String[] messages, String url) {
		renderDisptchJumpError(controller, messages, url, 3);
	}

	public static void renderDisptchJumpError(Controller controller, String[] messages, int seconds) {
		renderDisptchJumpError(controller, messages, null, seconds);
	}

	public static void renderDisptchJumpError(Controller controller, String[] messages, String url, int seconds) {
		if (null == url || url.isEmpty()) {
			url = "history.go(-1);";
		} else {
			url = "location.href='" + url + "';";
		}
		controller.setAttr("doActionScript", "<script>function doActionScript(){" + url + "}</script>");
		controller.setAttr("message", "<ol><li>" + String.join("</li><li>", messages) + "</li></ol>");
		controller.setAttr("seconds", seconds);
		controller.setAttr("status", false);
		controller.render("/view/common/dispatch_jump.html");
	}
	public static void redirectTopFrame(Controller controller,String url){
		url="location.href=\""+url+"\";";
		String script="<script type=\"text/javascript\">if(window.parent){window.parent."+url+"}else{"+url+"}</script>";
		ResponseUtil.write(controller.getResponse(), script);
		controller.renderNull();
	}
}
